package com.learn.springlearn.controller;

import com.learn.springlearn.constant.CarEnum;
import com.learn.springlearn.entity.CarDocument;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

@RestController
@RequestMapping("a")
public class A  {

    @Autowired
    B b;

    @Autowired
    C c;

    @Autowired
    private RestTemplate restTemplate;



    @PostConstruct
    public void init(){
        restTemplate.postForEntity("https://www.baidu.com",null,String.class);
//        throw new RuntimeException();
    }






    @GetMapping("hello")
    public String hello(String world) {

        return world;
//        return "{\"code\":"+world+"}";
    }

    @GetMapping("enum")
    public CarDocument req(HttpServletResponse response) {
        CarDocument carDocument = new CarDocument();
        carDocument.setCar(CarEnum.EXIST);
        return carDocument;
    }

    @GetMapping("/callB")
    public String callB(HttpServletRequest request, HttpServletResponse response){
//        Cookie cookie = new Cookie("token","123456");
//        response.addCookie(cookie);
//        Cookie[] cookies = request.getCookies();
//        Stream.of(cookies).forEach(cookie1 -> {
//            System.out.println(cookie1.getName() + " : "+cookie1.getValue());
//        });
        return b.sayHello();
    }

    @Async("taskExecutorPool")
    public Future<String> longTime()  {
        try {
            System.out.println(Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(5);
            System.out.println("执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>("执行完毕");
    }


}
